package com.valohyd.android_utils;

import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;


@SuppressWarnings("serial")
public class NSException extends Exception {

	private Context context;
	private Intent settingIntent;

	private String errorTitle;
	private String errorMessage;

	public NSException(Context context, Exception exception) {
		super(exception);
		this.errorTitle = exception != null ? exception.getMessage()
				: "NSException";
		this.errorMessage = exception != null ? exception.getMessage()
				: "NSException";
		this.context = context;
	}

	public NSException(Context context, String errorTitle, String errorMessage,
			Exception exception) {
		super(exception);
		this.errorTitle = errorTitle;
		this.errorMessage = errorMessage;
		this.context = context;
	}

	public NSException(Context context, int errorTitle, int errorMessage,
			Exception exception) {
		super(exception);
		this.context = context;
		setErrorTitle(errorTitle);
		setErrorMessage(errorMessage);
	}

	public Intent getSettingIntent() {
		return settingIntent;
	}

	public void setSettingIntent(Intent settingIntent) {
		this.settingIntent = settingIntent;
	}

	public void setErrorMessage(String errorMessage) {
		this.errorMessage = errorMessage;
	}

	public void setErrorTitle(String errorTitle) {
		this.errorTitle = errorTitle;
	}

	public void setErrorTitle(int resId) {
		this.errorTitle = getContext().getString(resId);
	}

	public void setErrorMessage(int resId) {
		this.errorMessage = getContext().getString(resId);
	}

	public String getErrorTitle() {
		return errorTitle;
	}

	public String getErrorMessage() {
		return errorMessage;
	}

	public Context getContext() {
		return context;
	}

	public OnClickListener getSettingButtonListener() {

		return settingIntent == null ? null : new OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				getContext().startActivity(settingIntent);
			}
		};
	}

	public void showAlertDialog() {

		EasyShowDialogs.showAlertDialogBoutons(context, errorTitle,
				errorMessage, context.getString(android.R.string.ok),
				"Settings", null, getSettingButtonListener());

	}

}
